// logger.h
/*
 * @Author       : Wangjunjun
 * @Date         : 2024-03-07
 * @copyleft Apache 2.0
 */
#ifndef LOGGER_H
#define LOGGER_H

#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>

// 日志相关操作的宏封装
#define INITLOG()      Logger::GetInstance()->Init()
#define UNINITLOG()      Logger::GetInstance()->UnInit()
#define GETLOGLEVEL()      Logger::GetInstance()->GetLogLevel()
#define SETLOGLEVEL(level) Logger::GetInstance()->SetLogLevel(level)
#define BASELOG(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, __func__}, level, __VA_ARGS__)
#define LOG_TRACE(...)     BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::trace, __VA_ARGS__)
#define LOG_DEBUG(...)     BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::debug, __VA_ARGS__)
#define LOG_INFO(...)      BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::info, __VA_ARGS__)
#define LOG_WARN(...)      BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::warn, __VA_ARGS__)
#define LOG_ERROR(...)     BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::err, __VA_ARGS__)
#define LOG_CRITICAL(...)  BASELOG(Logger::GetInstance()->getLogger(), spdlog::level::critical, __VA_ARGS__)

// 日志的配置项

// 日志的单例模式
class Logger {
public:
    //设置单例模式
    static std::shared_ptr<Logger>  GetInstance();

    //c++14返回值可设置为auto
    std::shared_ptr<spdlog::logger> getLogger() {
        return loggerPtr;
    }

    std::string GetLogLevel();

    void SetLogLevel(const std::string& level);

    void UnInit();

    void Init();
private:
    Logger();
    ~Logger();
    std::shared_ptr<spdlog::logger> loggerPtr;
    bool m_bInit;
    static std::shared_ptr<Logger> mInstance;
    static std::once_flag mflag;
    std::string m_level;
    std::string m_path;
    int64_t     m_size;
    int         m_count;
    std::string m_pattern;
};


#endif // LOGGER_H



